package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseCategoryTrademark;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.CategoryTrademarkVo;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper,BaseCategoryTrademark> implements BaseCategoryTrademarkService {

    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;

    @Autowired
    private BaseCategoryTrademarkMapper baseCategoryTrademarkMapper;

    @Override
    public List<BaseTrademark> findTrademarkList(Long category3Id) {

        //根据三级分类id 查询品牌分类中间表
        QueryWrapper<BaseCategoryTrademark> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category3_id",category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarks = baseCategoryTrademarkMapper.selectList(queryWrapper);

        //提取出品牌id
        if(baseCategoryTrademarks != null && baseCategoryTrademarks.size() > 0){

            //根据品牌id集合  查询品牌集合
            List<Long> idList = baseCategoryTrademarks.stream().map(BaseCategoryTrademark::getTrademarkId).collect(Collectors.toList());


            //返回品牌集合
           return baseTrademarkMapper.selectBatchIds(idList);
        }

        return null;
    }

    @Override
    public List<BaseTrademark> findCurrentTrademarkList(Long category3Id) {

        //关联的品牌id  根据三级分类id  查询品牌分类中间表
        QueryWrapper<BaseCategoryTrademark> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category3_id",category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarks = baseCategoryTrademarkMapper.selectList(queryWrapper);

        //获取关联品牌id
        if(baseCategoryTrademarks != null && baseCategoryTrademarks.size() > 0){
            List<Long> idsList = baseCategoryTrademarks.stream().map(BaseCategoryTrademark::getTrademarkId).collect(Collectors.toList());


            //查询所有的品牌
            List<BaseTrademark> baseTrademarkList = baseTrademarkMapper.selectList(null);

            if(baseTrademarkList != null && baseTrademarkList.size() > 0){

                return baseTrademarkList.stream().filter(baseTrademark -> {
                    return !idsList.contains(baseTrademark.getId());
                }).collect(Collectors.toList());
            }

        }

        //过滤掉关联的品牌

        return baseTrademarkMapper.selectList(null);
    }

    /**
     * 保存品牌关联对象
     * @param categoryTrademarkVo
     */
    @Override
    public void save(CategoryTrademarkVo categoryTrademarkVo) {
        //获取品牌id集合
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();

        //遍历品牌id
        if(trademarkIdList != null && trademarkIdList.size() > 0){
            List<BaseCategoryTrademark> baseCategoryTrademarkList = trademarkIdList.stream().map(trademarkId -> {
                //每次遍历过程中，创建品牌分类关联对象
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                baseCategoryTrademark.setTrademarkId(trademarkId);
                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());

                return baseCategoryTrademark;
            }).collect(Collectors.toList());


            //批量新增
            this.saveBatch(baseCategoryTrademarkList);

        }
    }
}
